//
//  RegisterModle.swift
//  XMGG
//
//  Created by iDev on 7/4/17.
//  Copyright © 2017 iDev. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

//“tu_account”:”登录账号”,
//“tu_password”:”用户密码”,
//“tu_name”:”用户姓名”,
//“tu_tel”:”手机号”,
//“tu_email”:”电子邮箱”,
//“tu_genDate”:”创建时间”,
//“tu_description”:”用户描述”,


struct RegisterModle {
    var tu_account:String?
    var tu_password:String?
    var tu_name: String?
    var tu_tel: String?
    var tu_email:String?
    var tu_genDate:String?
    var tu_description:String?
    var tu_verfcode:String?
    
    func registerRequest(){
        let parameters = [
            "req":[
                "tu_account":self.tu_account!,
                "tu_password":self.tu_password!,
                "tu_name":self.tu_name!,
                "tu_tel":self.tu_tel!,
                "tu_email":"1769712485@qq.com",
                "tu_genDate":self.tu_genDate!,
                "tu_description":"mmp sjbwy",
                "tu_verfcode":self.tu_verfcode!

            ]
        ]
        Alamofire.request(APIURL+"api/user/create", method: .post, parameters: parameters, encoding: JSONEncoding.default).response { (response) in
            guard let data = HasCommonNetWorkError(response: response) else{
                self.registerFail()
                return
            }
            self.handleData(data: data)
        }
    }
    
    private func handleData(data:Data){
        let json = JSON(data)
        guard let code = json["code"].int,let result = json["result"].dictionary else{
            registerFail()
            return
        }
        guard let msg = result["msg"]?.string else{
            registerFail()
            return
        }
        if code == 0 && msg == "user create success"{
            registerSuccess()
        }else{
            registerFail()
        }
        
    }
    
    private func registerSuccess(){
        UserDefaults.standard.set(tu_account, forKey: kNSDefaultUsername)
        let notification = Notification(name: Notification.Name(rawValue: "registerResult"), object: true, userInfo: nil)
        NotificationCenter.default.post(notification)
    }
    
    private func registerFail(){
        UserDefaults.standard.set(tu_account, forKey: kNSDefaultUsername)
        let notification = Notification(name: Notification.Name(rawValue: "registerResult"), object: false, userInfo: nil)
        NotificationCenter.default.post(notification)
    }
    
    func isLeagal()->Bool{
        let user = self.tu_account ?? ""
        let pwd = self.tu_password ?? ""
        let verfcode = self.tu_verfcode ?? ""
        //        let emailPattern = "^([a-zA-Z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
        let phonePattern = "^((13[0-9])|(17[0,6,7,8])|(15[^4,\\D])|(18[0,0-9])|(14[5,7]))\\d{8}$"
        //        let emailPredicate = NSPredicate(format: "SELF MATCHES %@", argumentArray: [emailPattern])
        let phonePredicate = NSPredicate(format: "SELF MATCHES %@", argumentArray: [phonePattern])
        return (phonePredicate.evaluate(with:user))&&(pwd.characters.count>7&&pwd.characters.count<17&&(verfcode.characters.count>3))
    }
}
